package com.file.system.entity.statistics;


import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.List;


/**
 * 首页统计
 *
 * @author jch
 */
@Data
@Accessors(chain = true)
public class ComprehensiveStatistics implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 今日
     */
    private Details today;
    /**
     * 昨日
     */
    private Details yesterday;
    /**
     * 本周
     */
    private Details week;
    /**
     * 本月
     */
    private Details month;
    /**
     * 上月
     */
    private Details lastMonth;
    /**
     * 全部
     */
    private Details all;
    /**
     * 平均
     */
    private Details avgPerDay;
    /**
     * 目标日期
     */
    private String targetDateTime;
    /**
     * 天数
     */
    private long days;
    /**
     * 近30天
     */
    private List<Details> calculateLast30Days;


    @Data
    public class Details {
        /**
         * 统计名称
         */
        private String name;
        /**
         * pv数量
         */
        private Long pvTotal;
        /**
         * ip数量
         */
        private Long ipTotal;
        /**
         * pv比例
         */
        private String pvPercentage;
        /**
         * ip比例
         */
        private String ipPercentage;
    }

    public static Details init(String name, long pvTotal, long ipTotal) {
        ComprehensiveStatistics statistics = new ComprehensiveStatistics();
        ComprehensiveStatistics.Details statisticsDetails = statistics.new Details();

        statisticsDetails.setName(name);
        statisticsDetails.setPvTotal(pvTotal);
        statisticsDetails.setIpTotal(ipTotal);

        return statisticsDetails;
    }

    public static Details init(String name, long total, long totalIpCount, long pvTotal, long ipTotal) {
        ComprehensiveStatistics statistics = new ComprehensiveStatistics();
        ComprehensiveStatistics.Details statisticsDetails = statistics.new Details();

        statisticsDetails.setName(name);
        statisticsDetails.setPvTotal(pvTotal);
        statisticsDetails.setIpTotal(ipTotal);
        statisticsDetails.setPvPercentage(calculatePercentage(total, pvTotal));
        statisticsDetails.setIpPercentage(calculatePercentage(totalIpCount, ipTotal));

        return statisticsDetails;
    }


    private static String calculatePercentage(long total, long completeCount) {
        if (total == 0) {
            return "0.00";
        } else {
            double percent = ((double) completeCount / total) * 100;
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            return decimalFormat.format(percent);
        }
    }


}
